<?php
/**
*	sso登录
*	访问地址如：http://demo.rockoa.com/api.php?m=opensso
*/
class openssoClassAction extends openapiAction
{
	protected 	$keycheck= false;
	
	public function indexAction()
	{
		$ssotoken	= $this->get('ssotoken');
		$lurl		= urldecode($this->get('backurl')); //登录成功跳转地址urlencode
		if(isempt($ssotoken))return '没有参数ssotoken';
		
		$checkurl 	= getconfig('sso_checkurl'); //验证地址
		$ssokey 	= getconfig('sso_key'); //验证key
		
		if(isempt($checkurl))return '没有配置验证地址';
		$jg 	= contain($checkurl,'?')?'&':'?';
		
		$url 	= $checkurl.$jg.'ssotoken='.$ssotoken.'&ssokey='.$ssokey.'';
		$result	= c('curl')->getcurl($url); //要返回用户帐号/手机号
		if(isempt($result))return '验证失败没有返回值';
		if(!contain($result,'id'))return '验证失败：'.$result.'';
		$uarr 	= json_decode($result,true);
		$xinhuid= (int)$uarr['id'];
		$user   = $uarr['user'];
		$name   = $uarr['name'];
		if(isempt($user))$user = 'xinhu'.$xinhuid.'';
		if(isempt($name))$name = '信呼用户';
		$db	= m('users');
		$where = '`xinhuid`='.$xinhuid.'';
		$rs 	= $db->getone($where);
		$uid 	= 0;
		$iuarr	= array(
			'xinhuid' 	=> $xinhuid,
			'status'	=> 1,
			//'email' 	=> $uarr['email'],
			//'pass' 	=> $uarr['pass'],
			//'rand' 	=> $uarr['rand'],
			//'mobile' => $uarr['mobile'],
			'type' 	=> $uarr['type'],
			'face' 	=> $uarr['face'],
		);
		if(!$rs){
			$iuarr['name'] = $name;
			$iuarr['user'] = $user;
			$iuarr['regdt'] = $this->rock->now;
			$where = '';
		}else{
			$uid = $rs['id'];
		}
		
		$suaid = $db->record($iuarr, $where);
		if($uid==0)$uid = $this->db->insert_id();
		
		$this->rock->savesession(array(
			'adminid'	=> $uid,
		));
		$this->rock->location('index.php?m=index');
	
		return 'success';
	}
}	